package com.naihe.anderson.sms.activity;

import java.util.ArrayList;
import java.util.Date;
import java.util.List;

import android.app.Activity;
import android.content.Intent;
import android.os.Bundle;
import android.os.Handler;
import android.os.Message;
import android.text.TextUtils;
import android.view.KeyEvent;
import android.view.View;
import android.view.View.OnClickListener;
import android.widget.AdapterView;
import android.widget.AdapterView.OnItemClickListener;
import android.widget.AdapterView.OnItemLongClickListener;
import android.widget.Button;
import android.widget.EditText;
import android.widget.ImageButton;
import android.widget.ListView;
import android.widget.Toast;

import com.naihe.anderson.sms.R;
import com.naihe.anderson.sms.adapter.CapitalInfoAdapter;
import com.naihe.anderson.sms.application.NhSmsApp;
import com.naihe.anderson.sms.entity.CapitalInfo;
import com.naihe.anderson.sms.entity.NhsmsSession;
import com.naihe.anderson.sms.listener.OnCapitalListener;
import com.naihe.anderson.sms.manager.NhSmsMgr;
import com.naihe.anderson.sms.manager.RecordMoneyMgr;
import com.naihe.anderson.sms.widgets.LongClickDialog;
import com.naihe.anderson.sms.widgets.SystemAlertDialog;

public class CapitalNumberInfoActivity extends Activity implements OnClickListener,OnItemClickListener, OnItemLongClickListener, OnCapitalListener{

	NhSmsApp nhSmsApp;
	RecordMoneyMgr moneyMgr;
	NhSmsMgr nhSmsMgr;
	
	Button btn_back;
	Button btn_explain;
	ImageButton add_btn;
	EditText info_number_et;
	EditText expenditure_keys_et;
	EditText income_keys_et;
	
	CapitalInfo capitalInfo;
	
	ListView capital_list;
	CapitalInfoAdapter capitalInfoAdapter;
	List<CapitalInfo> capitalInfoList = new ArrayList<CapitalInfo>();
	
	public static final int MSG_WHAT_DATECHANGE_ACTIVITY = 0;
	public static final int MSG_WHAT_TO_SAVE_OLD = 1;
	public static final int MSG_WHAT_NOT_TO_SAVE_OLD = 2;
	public static final int MSG_WHAT_TO_DEL_OLD = 3;
	public static final int MSG_WHAT_NOT_TO_DEL_OLD = 4;
	Handler mHandler = new Handler(){

		@Override
		public void handleMessage(Message msg) {
			switch (msg.what) {
			case MSG_WHAT_DATECHANGE_ACTIVITY:
				capitalInfoList.clear();
				capitalInfoList.addAll(moneyMgr.getCapitalInfoList());
				capitalInfoAdapter.notifyDataSetChanged();
				
				break;
			case MSG_WHAT_TO_SAVE_OLD:
				if(capitalInfo != null)
					moneyMgr.insertCapotal(capitalInfo, true);
				break;
			case MSG_WHAT_NOT_TO_SAVE_OLD:
				if(capitalInfo != null)
					moneyMgr.insertCapotal(capitalInfo, false);
				break;
			case MSG_WHAT_TO_DEL_OLD:
				CapitalInfo capitalInfo1 = (CapitalInfo) msg.obj;
				if(capitalInfo1 != null)
					moneyMgr.deleteCapotal(capitalInfo1, true);
				break;
			case MSG_WHAT_NOT_TO_DEL_OLD:
				CapitalInfo capitalInfo2 = (CapitalInfo) msg.obj;
				if(capitalInfo2 != null)
					moneyMgr.deleteCapotal(capitalInfo2, false);
				break;
			default:
				break;
			}
		}
	};
	
	@Override
	public void onCreate(Bundle savedInstanceState) {
		super.onCreate(savedInstanceState);
		setContentView(R.layout.capital_main_layout);

		nhSmsApp = (NhSmsApp) getApplication();
		nhSmsApp.setActivity(this);
		moneyMgr = nhSmsApp.getRecordMoneyMgr();
		moneyMgr.setOnCapitalListener(this);
		
		initView();
		
		mHandler.sendEmptyMessageDelayed(MSG_WHAT_DATECHANGE_ACTIVITY, 100);
	}
	
	private void initView(){
		btn_back = (Button) findViewById(R.id.btn_back);
		btn_back.setOnClickListener(this);
		btn_explain = (Button) findViewById(R.id.btn_explain);
		btn_explain.setOnClickListener(this);
		
		add_btn = (ImageButton) findViewById(R.id.add_btn);
		add_btn.setOnClickListener(this);
		
		info_number_et = (EditText) findViewById(R.id.info_number_et);
		expenditure_keys_et = (EditText) findViewById(R.id.expenditure_keys_et);
		income_keys_et = (EditText) findViewById(R.id.income_keys_et);
		
		capital_list = (ListView) findViewById(R.id.capital_list);
		capitalInfoAdapter = new CapitalInfoAdapter(CapitalNumberInfoActivity.this, capitalInfoList);
		capital_list.setAdapter(capitalInfoAdapter);
		capital_list.setOnItemClickListener(this);
		capital_list.setOnItemLongClickListener(this);
	}
	
	private void addCapital(){
		String info_number = info_number_et.getText().toString();
		if(TextUtils.isEmpty(info_number)){
			Toast.makeText(CapitalNumberInfoActivity.this, "号码没有填写", Toast.LENGTH_SHORT).show();
			info_number_et.requestFocus();
			return;
		}
		String expenditure_keys = expenditure_keys_et.getText().toString();
		String income_keys = income_keys_et.getText().toString();
		if(TextUtils.isEmpty(expenditure_keys) && TextUtils.isEmpty(income_keys)){
			Toast.makeText(CapitalNumberInfoActivity.this, "关键字至少填写一项", Toast.LENGTH_SHORT).show();
			expenditure_keys_et.requestFocus();
			return;
		}
		
		if(!TextUtils.isEmpty(expenditure_keys) && expenditure_keys.indexOf("*") < 0){
			Toast.makeText(CapitalNumberInfoActivity.this, "支出关键字填写错误", Toast.LENGTH_SHORT).show();
			expenditure_keys_et.requestFocus();
			return;
		}
		
		if(!TextUtils.isEmpty(income_keys) && income_keys.indexOf("*") < 0){
			Toast.makeText(CapitalNumberInfoActivity.this, "收入关键字填写错误", Toast.LENGTH_SHORT).show();
			income_keys_et.requestFocus();
			return;
		}
		
		info_number_et.setText("");
		expenditure_keys_et.setText("");
		income_keys_et.setText("");
		
		capitalInfo = new CapitalInfo();
		capitalInfo.number = info_number;
		capitalInfo.expenditureKeys = expenditure_keys;
		capitalInfo.incomeIeys = income_keys;
		capitalInfo.cid = new Date().getTime();
		capitalInfo.createTime = capitalInfo.cid;
		
		Message okMessage = new Message();
		okMessage.what = MSG_WHAT_TO_SAVE_OLD;
		
		Message cancelMessage = new Message();
		cancelMessage.what = MSG_WHAT_NOT_TO_SAVE_OLD;
		
		new SystemAlertDialog(CapitalNumberInfoActivity.this, mHandler, "", "\r\n是否查询之前的短信记录保存？", " 否 ", "保存", okMessage, cancelMessage).show();
	}
	
	private static final int ITEM_LONG_CLICK_DEL = 0;
	private static final int ITEM_LONG_CLICK_DETAIL = 1;
	@Override
	public boolean onItemLongClick(AdapterView<?> parent, View view,
			int position, long id) {
		final CapitalInfo capitalInfo = capitalInfoAdapter.getItem(position);
		if(capitalInfo == null) return false;
		
		String[] links = new String[]{"删除","短信查看"};
		new LongClickDialog(CapitalNumberInfoActivity.this, links, getResources().getString(R.string.operation), new OnItemClickListener() {
			@Override
			public void onItemClick(AdapterView<?> parent, View view, int position, long id) {
				if(position == ITEM_LONG_CLICK_DEL){
					Message okMessage = new Message();
					okMessage.obj = capitalInfo;
					okMessage.what = MSG_WHAT_TO_DEL_OLD;
					
					Message cancelMessage = new Message();
					cancelMessage.obj = capitalInfo;
					cancelMessage.what = MSG_WHAT_NOT_TO_DEL_OLD;
					
					new SystemAlertDialog(CapitalNumberInfoActivity.this, mHandler, "", "\r\n是否删除之前账本保存记录？", " 否 ", "删除", okMessage, cancelMessage).show();
				} else if(position == ITEM_LONG_CLICK_DETAIL){
					NhsmsSession nhsmsSession = nhSmsMgr.getSessionByAddress(capitalInfo.number);
					if(nhsmsSession != null){
						Intent intent = new Intent(CapitalNumberInfoActivity.this, TalkMessageActivity.class);
						intent.putExtra("NhsmsSession", nhsmsSession);
						CapitalNumberInfoActivity.this.startActivity(intent);
					}
				}
			}
		}).show();
		return false;
	}

	@Override
	public void onItemClick(AdapterView<?> parent, View view, int position,
			long id) {
		CapitalInfo capitalInfo = capitalInfoAdapter.getItem(position);
		if(capitalInfo == null) return;
		
		if(nhSmsMgr == null)
			nhSmsMgr = nhSmsApp.getNhSmsMgr();
		
//		Intent intent = new Intent(CapitalNumberInfoActivity.this, TalkMessageActivity.class);
//		CapitalNumberInfoActivity.this.startActivity(intent);
	}

	@Override
	public void onClick(View v) {
		if(v.getId() == R.id.btn_back){
			toFinishActivity();
		} else if(v.getId() == R.id.add_btn){
			addCapital();
		} else if(v.getId() == R.id.btn_explain){
			Intent intent = new Intent(CapitalNumberInfoActivity.this, ExplainActivity.class);
			intent.putExtra("fromActivity", "CapitalNumberInfoActivity");
			CapitalNumberInfoActivity.this.startActivity(intent);
		}
	}
	
	@Override
	public boolean onKeyDown(int keyCode, KeyEvent event) {
		if (keyCode == KeyEvent.KEYCODE_BACK) {
			toFinishActivity();
			return true;
		}

		return super.onKeyDown(keyCode, event);
	}

	private void toFinishActivity(){
		nhSmsApp.setActivity(null);
		moneyMgr.setOnCapitalListener(null);
		CapitalNumberInfoActivity.this.finish();
		System.gc();
	}

	@Override
	public void onDataChanged() {
		mHandler.sendEmptyMessage(MSG_WHAT_DATECHANGE_ACTIVITY);
	}
}
